<?php
class pRoleAction extends pBaseAction{

    public function index(){
        $dao=new RoleModel();
        $count=$dao->count();
        if($count>0){
            require LIB_PATH.'Util/PageEx.class.php';
            $p=new PageEx($count,12);
            $this->assign('list',$dao->limit($p->firstRow.','.$p->listRows)->select());
            $this->assign('page',$p->show());
        }
        $this->display();
    }

    public function add(){
        $menuDao=new MenuModel();
        $this->assign('popelist',$menuDao->getPopedomTree());
        $this->display();
    }

    public function save(){
        $dao = new RoleModel();
        $rs = $dao->addRole();
        $this->simpleReturn($dao,$rs,sprintf(L('module_save_role_ok'),$rs['name']));
    }

    public function removal(){
        //@todo
        echo 'remove role ok';
    }

    public function edit(){
        $roleId=$this->getId();
        if(false!==$roleId){
            $dao=new RoleModel();
            $rs=$dao->where("id='{$roleId}'")->find();
            if($rs){
                $menuDao=new MenuModel();
                $this->assign('popelist',$menuDao->getPopedomTree());
                //获取权限信息
                $tablePrefix=C('db_prefix');
                $popes=$menuDao->field('group_concat(distinct n.menu) menus')
                                ->where("a.role='{$roleId}' and n.id=a.nodeId and n.menu>0")
                                ->table($tablePrefix.'access a,'.$tablePrefix.'node n')
                                ->findAll();
                $this->assign('popes', $popes[0]['menus']);
                $this->assign('rs', $rs);
                $this->display();
                return;
            }
        }
        $this->fail(L('module_edit_role_fail'),L('module_no_such_role'));
    }

    public function update() {
        $dao=new RoleModel();
        $rs=$dao->updateRole();
        $this->simpleReturn($dao,$rs,sprintf(L('module_update_role_ok'),$rs['name']));
    }
}
